package com.project.lk.nurseapp.utils;

import android.app.AlarmManager;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */

public class TimeUtils {

    //时间格式转换成yyyy/mm
    public static String getSateTime(Date date){
        SimpleDateFormat newFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String newTime = newFormat.format(date);
        return newTime;
    }

    public static long getWeekCircle(String dateValue,
                              Calendar calendar){
        final long now = System.currentTimeMillis();
        long nextTime = 0;
            final long[] checkedWeeks = parseDateWeeks(dateValue);

            if (null != checkedWeeks) {
                for (long week : checkedWeeks) {
                    calendar.set(Calendar.DAY_OF_WEEK, (int) (week + 1));
                    long triggerAtTime = calendar.getTimeInMillis();
                    if (triggerAtTime <= now) { // 下周
                        triggerAtTime += AlarmManager.INTERVAL_DAY * 7;
                    }
                    // 保存最近闹钟时间
                    if (0 == nextTime) {
                        nextTime = triggerAtTime;
                    } else {
                        nextTime = Math.min(triggerAtTime, nextTime);
                    }
                }
            }
            return nextTime;

    }

    public static long[] parseDateWeeks(String value) {
        String[] split = value.split(" ");
        for (int i=0;i<split.length;i++){
            if (value.contains("一")) {
                value = value.replace("一", "1");
            }else if (value.contains("二")){
                value= value.replace("二","2");
            }else if (value.contains("三")){
                value=  value.replace("三","3");
            }else if (value.contains("四")){
                value=  value.replace("四","4");
            }else if (value.contains("五")){
                value= value.replace("五","5");
            }else if (value.contains("六")){
                value= value.replace("六","6");
            }else if (value.contains("日")){
                value= value.replace("日","7");
            }
        }
        Log.e("MLOG",value);
        long[] weeks = null;
        try {
            final String[] items = value.split(" ");
            weeks = new long[items.length];
            int i = 0;
            for (String s : items) {
                weeks[i++] = Long.valueOf(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return weeks;
    }
}
